#include<QLineEdit>
#include<QVBoxLayout>
#include<QLabel>
#include<QPushButton>
#include<QTableWidget>
#include<QFile>
#include<QMessageBox>
#include<QTextCodec>

#include "tageditdialog.h"
#include "mainwindow.h"
#include"musictag.h"
TagEditDialog::TagEditDialog(QWidget *parent)
    :QDialog(parent)
{
    //QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    Title = new QLineEdit(this);
    Album = new QLineEdit(this);
    Artist = new QLineEdit(this);
    Year = new QLineEdit(this);
    Genre = new QLineEdit(this);

    title = new QLabel(tr("Title"),this);
    artist = new QLabel(tr("Artist"),this);
    album = new QLabel(tr("Album"),this);
    year = new QLabel(tr("year"),this);
    genre = new QLabel(tr("Genre"),this);

    ButtonOK = new QPushButton(tr("OK"),this);
    //ButtonOK->setEnabled(false);
    ButtonCancel = new QPushButton(tr("cancel"),this);
    ButtonCancel->setFixedWidth(ButtonOK->width());

    Layout = new QGridLayout(this);
    Layout->addWidget(Title,0,1);
    Layout->addWidget(Artist,1,1);
    Layout->addWidget(Album,2,1);
    Layout->addWidget(Genre,3,1);
    Layout->addWidget(Year,4,1);

    Layout->addWidget(title,0,0);
    Layout->addWidget(artist,1,0);
    Layout->addWidget(album,2,0);
    Layout->addWidget(genre,3,0);
    Layout->addWidget(year,4,0);

    Layout->addWidget(ButtonOK,5,0);
    Layout->addWidget(ButtonCancel,5,1);

    //connect(Title,SIGNAL(textChanged(QString)),ButtonOK,SLOT(setEnabled(bool)));

    connect(ButtonOK,SIGNAL(clicked()),this,SLOT(saveChange()));
    connect(ButtonCancel,SIGNAL(clicked()),this,SLOT(close()));
    this->setLayout(Layout);
    this->setFixedSize(400,300);

}

int TagEditDialog::setMusicTable(QTableWidget *Table)
{
    if(Table){
        this->Table = Table;
        RowOfSong = Table->selectedItems()[0]->row();
        Title->setText(Table->item(RowOfSong,MainWindow::TITLE)->data(Qt::DisplayRole).toString());
        Artist->setText(Table->item(RowOfSong,MainWindow::ARTIST)->data(Qt::DisplayRole).toString());
        Album->setText(Table->item(RowOfSong,MainWindow::ALBUM)->data(Qt::DisplayRole).toString());
        Genre->setText(Table->item(RowOfSong,MainWindow::GENRE)->data(Qt::DisplayRole).toString());
        Year->setText(Table->item(RowOfSong,MainWindow::YEAR)->data(Qt::DisplayRole).toString());
        FileDir = Table->item(RowOfSong,MainWindow::LOCATION)->data(Qt::DisplayRole).toString();
        return true;
    }
    else return false;
}

void TagEditDialog::saveChange()
{
    Table->item(RowOfSong,MainWindow::TITLE)->setData(Qt::DisplayRole,Title->text());
    Table->item(RowOfSong,MainWindow::ARTIST)->setData(Qt::DisplayRole,Artist->text());
    Table->item(RowOfSong,MainWindow::ALBUM)->setData(Qt::DisplayRole,Album->text());
    Table->item(RowOfSong,MainWindow::GENRE)->setData(Qt::DisplayRole,Genre->text());
    Table->item(RowOfSong,MainWindow::YEAR)->setData(Qt::DisplayRole,Year->text());
    Table->update();
/*
    QFile tempFile(FileDir);
    if(!tempFile.isWritable()){
        QMessageBox MB(this);
        MB.setText(tr("this is a readonly file,so the changes will only be in the playlist not in the file"));
        MB.show();
        tempFile.close();
    }
*/
 //   else{
        MusicTag file(FileDir,true);

        file.setTitle(Title->text(),true);
        file.setArtist(Artist->text(),true);
        file.setAlbum(Album->text(),true);
        file.setGenre(Genre->text(),true);
        file.setYear(Year->text().toInt(),true);
        //file.writeTag();
  //  }

    this->destroy(true,true);
}

